package lion.framework.core.web.views;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import lion.dev.lang.Lang;
import lion.framework.core.conf.Config;
import lion.framework.core.conf.ConfigManager;
import lion.framework.core.web.anno.ViewType;

/**
 * 显示html信息
 *
 * 请求中包含参数：
 * 
 * @param content
 *            html内容
 *
 *
 *            <pre>
 * time   2013 2013年12月23日 下午5:05:08
 * mail   hl_0074@sina.com
 *            </pre>
 *
 * @author lion
 */
@ViewType("html")
public class HtmlView implements IView {

	private String name;
	private String content;

	private static final String VIEW_TYPE = "html";

	public HtmlView() {

	}

	public HtmlView(String content) {

		this.content = content;
	}

	@Override
	public void render(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String content = Lang.nvl((String) request.getAttribute("content"), this.content);
		if (StringUtils.isBlank(content)) { return; }

		response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate,max-age=0,proxy-revalidate,no-transform,pre-check=0,post-check=0,private");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "0");
		Config config = ConfigManager.getConfig("framework");
		response.setContentType("text/html; charset=" + config.getString("framework.sysencoding", "UTF-8"));
		try {
			response.getWriter().write(content);
		} catch (IOException e) {
		}
	}

	@Override
	public String getName() {

		return name;
	}

	@Override
	public String type() {

		return VIEW_TYPE;
	}

}
